<template>
  <div class="blacklist-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>已封禁用户列表</span>
          <div class="search-box">
            <el-input
              v-model="searchKeyword"
              placeholder="搜索用户名/昵称/邮箱"
              prefix-icon="Search"
              clearable
              @clear="handleSearch"
              @keyup.enter="handleSearch"
            />
            <el-button type="primary" @click="handleSearch">搜索</el-button>
          </div>
        </div>
      </template>

      <!-- 已封禁用户列表 -->
      <el-table
        :data="userList"
        style="width: 100%"
        v-loading="loading"
        border
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column label="用户信息" min-width="200">
          <template #default="scope">
            <div class="user-info">
              <el-avatar :size="32" :src="scope.row.avatar"></el-avatar>
              <div class="user-details">
                <div class="username">{{ scope.row.username }}</div>
                <div class="email">{{ scope.row.email }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="blockReason" label="封禁原因" min-width="200">
          <template #default="scope">
            <el-tooltip :content="scope.row.blockReason" placement="top" :show-after="1000">
              <span class="truncate-text">{{ scope.row.blockReason }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="blockTime" label="封禁时间" width="150" />
        <el-table-column prop="blockDuration" label="封禁时长" width="120">
          <template #default="scope">
            {{ scope.row.blockDuration === '-1' ? '永久' : `${scope.row.blockDuration}天` }}
          </template>
        </el-table-column>
        <el-table-column prop="unblockTime" label="解封时间" width="150">
          <template #default="scope">
            {{ scope.row.blockDuration === '-1' ? '永久封禁' : scope.row.unblockTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button type="success" size="small" @click="unblockUser(scope.row)">
              解除封禁
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页器 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<!--<script setup>-->
<!--import { ref, onMounted } from 'vue'-->
<!--import { ElMessage, ElMessageBox } from 'element-plus'-->

<!--// 表格数据-->
<!--const loading = ref(false)-->
<!--const userList = ref([])-->
<!--const total = ref(0)-->
<!--const currentPage = ref(1)-->
<!--const pageSize = ref(10)-->
<!--const searchKeyword = ref('')-->

<!--// 加载被封禁用户列表-->
<!--const loadBlacklistUsers = async () => {-->
<!--  loading.value = true-->
<!--  try {-->
<!--    // 模拟异步请求-->
<!--    setTimeout(() => {-->
<!--      // 这里应该是实际的API调用-->
<!--      // const response = await fetch(`/api/admin/blacklist?page=${currentPage.value}&pageSize=${pageSize.value}&keyword=${searchKeyword.value}`)-->
<!--      // const data = await response.json()-->
<!--      // userList.value = data.list-->
<!--      // total.value = data.total-->
<!--      -->
<!--      // 模拟数据-->
<!--      const mockData = []-->
<!--      const blockReasons = [-->
<!--        '发布违规内容',-->
<!--        '恶意灌水',-->
<!--        '侮辱/攻击其他用户',-->
<!--        '发布广告信息',-->
<!--        '违反社区规范'-->
<!--      ]-->
<!--      -->
<!--      const blockDurations = ['3', '7', '30', '-1']-->
<!--      -->
<!--      for (let i = 1; i <= pageSize.value; i++) {-->
<!--        const id = (currentPage.value - 1) * pageSize.value + i-->
<!--        const blockTime = new Date(Date.now() - Math.floor(Math.random() * 30) * 24 * 3600 * 1000).toLocaleDateString()-->
<!--        const blockDuration = blockDurations[Math.floor(Math.random() * blockDurations.length)]-->
<!--        -->
<!--        let unblockTime = '永久封禁'-->
<!--        if (blockDuration !== '-1') {-->
<!--          const unblockDate = new Date(new Date(blockTime).getTime() + parseInt(blockDuration) * 24 * 3600 * 1000)-->
<!--          unblockTime = unblockDate.toLocaleDateString()-->
<!--        }-->
<!--        -->
<!--        mockData.push({-->
<!--          id,-->
<!--          username: `user${id}`,-->
<!--          nickname: `用户${id}`,-->
<!--          email: `user${id}@example.com`,-->
<!--          avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',-->
<!--          blockReason: blockReasons[Math.floor(Math.random() * blockReasons.length)],-->
<!--          blockTime,-->
<!--          blockDuration,-->
<!--          unblockTime-->
<!--        })-->
<!--      }-->
<!--      -->
<!--      userList.value = mockData-->
<!--      total.value = 50 // 假设总数为50-->
<!--      -->
<!--      loading.value = false-->
<!--    }, 500)-->
<!--  } catch (error) {-->
<!--    console.error('加载已封禁用户列表失败:', error)-->
<!--    loading.value = false-->
<!--  }-->
<!--}-->

<!--// 首次加载-->
<!--onMounted(() => {-->
<!--  loadBlacklistUsers()-->
<!--})-->

<!--// 处理页码变化-->
<!--const handleCurrentChange = (val) => {-->
<!--  currentPage.value = val-->
<!--  loadBlacklistUsers()-->
<!--}-->

<!--// 处理每页条数变化-->
<!--const handleSizeChange = (val) => {-->
<!--  pageSize.value = val-->
<!--  currentPage.value = 1-->
<!--  loadBlacklistUsers()-->
<!--}-->

<!--// 搜索-->
<!--const handleSearch = () => {-->
<!--  currentPage.value = 1-->
<!--  loadBlacklistUsers()-->
<!--}-->

<!--// 解除封禁-->
<!--const unblockUser = async (user) => {-->
<!--  try {-->
<!--    // 确认操作-->
<!--    await ElMessageBox.confirm(`确定要解除对用户 "${user.username}" 的封禁吗?`, '提示', {-->
<!--      confirmButtonText: '确定',-->
<!--      cancelButtonText: '取消',-->
<!--      type: 'warning'-->
<!--    })-->
<!--    -->
<!--    // 模拟API调用-->
<!--    // await fetch(`/api/admin/users/${user.id}/unblock`, { method: 'PUT' })-->
<!--    -->
<!--    // 从列表中移除-->
<!--    userList.value = userList.value.filter(item => item.id !== user.id)-->
<!--    total.value&#45;&#45;-->
<!--    -->
<!--    ElMessage.success('已解除封禁')-->
<!--  } catch (error) {-->
<!--    if (error !== 'cancel') {-->
<!--      console.error('解除封禁失败:', error)-->
<!--      ElMessage.error('操作失败，请重试')-->
<!--    }-->
<!--  }-->
<!--}-->
<!--</script>-->

<style scoped>
.blacklist-container {
  padding: 0 10px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-box {
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-details {
  display: flex;
  flex-direction: column;
}

.email {
  font-size: 12px;
  color: #909399;
}

.truncate-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: inline-block;
  max-width: 250px;
}
</style> 